package com.ahsh1688.zhengshi.window;

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import javax.swing.*;
import javax.swing.event.*;

import com.ahsh1688.zhengshi.system.entity.TblUserinfo;
import com.ahsh1688.zhengshi.system.service.TblUserinfoService;
import net.miginfocom.swing.*;
import org.springframework.beans.factory.annotation.Autowired;
/*
 * Created by JFormDesigner on Tue Dec 19 10:08:37 CST 2023
 */



/**
 * @author Administrator
 */
public class LoginWindow extends JFrame {

    @Autowired
    private TblUserinfoService tblUserinfoService;

	public LoginWindow() {
		initComponents();
	}

	private void btnLogin(ActionEvent e) {
		// TODO add your code here
		if(txtUserName.getText().trim().equals("")){
			JOptionPane.showMessageDialog(this, "请输入用户名!", "提示", JOptionPane.WARNING_MESSAGE);
			txtUserName.requestFocus();
			return;
		}
		if(txtPassword.getPassword().length == 0){
			JOptionPane.showMessageDialog(this, "请输入密码！", "提示", JOptionPane.INFORMATION_MESSAGE);
			txtPassword.requestFocus();
			return;
		}
		//autentication username and password
        TblUserinfo tblUserinfo = new TblUserinfo();
		tblUserinfo.setUserName(txtUserName.getText().trim());
		char[] password = txtPassword.getPassword();
        System.out.println("密码：" + new String(password));
		tblUserinfo.setUserPwd(new String(password));
        List<TblUserinfo> list = tblUserinfoService.findList(tblUserinfo);
        if(list.size() > 0){
            dispose();
        }else{
            JOptionPane.showMessageDialog(this, "用户名或者密码不正确！", "错误提示", JOptionPane.WARNING_MESSAGE);
        }
    }

	private void btnClose(ActionEvent e) {
		// TODO add your code here
		System.exit(0);
	}

    private void chkShowPasswordItemStateChanged(ItemEvent e) {
        // TODO add your code here
        if(e.getStateChange() == ItemEvent.SELECTED){
            txtPassword.setEchoChar((char)0);
        }else{
            txtPassword.setEchoChar('*');
        }
    }

	private void initComponents() {

		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        txtUserName = new JTextField();
        label2 = new JLabel();
        txtPassword = new JPasswordField();
        chkShowPassword = new JCheckBox();
        btnLogin = new JButton();
        btnClose = new JButton();

        //======== this ========
        setLayout(new MigLayout(
            "insets 0,hidemode 3,gap 5 5",
            // columns
            "[27,fill]" +
            "[71,fill]" +
            "[88,fill]" +
            "[85,fill]" +
            "[51,fill]" +
            "[33,fill]",
            // rows
            "[30,fill]" +
            "[fill]" +
            "[fill]" +
            "[7,fill]" +
            "[43,fill]" +
            "[fill]"));

        //---- label1 ----
        label1.setText("\u7528\u6237\u540d\uff1a");
        label1.setHorizontalAlignment(SwingConstants.RIGHT);
        label1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 14));
        add(label1, "cell 1 1");

        //---- txtUserName ----
        txtUserName.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 14));
        add(txtUserName, "cell 2 1 2 1");

        //---- label2 ----
        label2.setText("\u5bc6\u7801\uff1a");
        label2.setHorizontalAlignment(SwingConstants.RIGHT);
        label2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 14));
        add(label2, "cell 1 2");
        add(txtPassword, "cell 2 2 2 1");

        //---- chkShowPassword ----
        chkShowPassword.setText("\u663e\u793a\u5bc6\u7801");
        chkShowPassword.addItemListener(e -> chkShowPasswordItemStateChanged(e));
        add(chkShowPassword, "cell 4 2");

        //---- btnLogin ----
        btnLogin.setText("\u767b\u5f55");
        btnLogin.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 14));
        btnLogin.addActionListener(e -> btnLogin(e));
        add(btnLogin, "cell 2 4");

        //---- btnClose ----
        btnClose.setText("\u5173\u95ed");
        btnClose.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 14));
        btnClose.addActionListener(e -> btnClose(e));
        add(btnClose, "cell 3 4");
		// JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JTextField txtUserName;
    private JLabel label2;
    private JPasswordField txtPassword;
    private JCheckBox chkShowPassword;
    private JButton btnLogin;
    private JButton btnClose;
	// JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
